package com.ly.user.web;

import javax.servlet.http.HttpServletRequest;

import com.ly.login.bean.LoginProperties;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Enumeration;

@RestController
@RefreshScope
public class UserDo {

    @RequestMapping("user")
    String user(HttpServletRequest request) {
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            System.out.println(headerName + ":" + headerValue);
        }
        return "user " + request.getServerPort();
    }

    @Autowired
    LoginProperties loginProperties;

    @Value("${login.usernameColumn}")
    String usernameColumn;

    @RequestMapping("userProps")
    LoginProperties getLoginProperties() {
        return loginProperties;
    }

    ///////////////////////////////////////////////
    @RabbitListener(queuesToDeclare = @Queue("work.queue"))
    public void recvMsg1(String msg){
        System.out.println("msg1 = " + msg);
    }

    @RabbitListener(queuesToDeclare = @Queue("work.queue"))
    public void recvMsg2(String msg){
        System.out.println("msg2 = " + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topicQueue"),
            exchange = @Exchange(type = ExchangeTypes.TOPIC, name = "topicEx"),
            key = {"user.*","cart.#"}
    ))
    public void topicMsg1(String msg){
        System.out.println("topicMsg1 = " + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            exchange = @Exchange(type = ExchangeTypes.TOPIC, name = "topicEx"),
            key = {"user.save","index.*"}
    ))
    public void topicMsg2(String msg){
        System.out.println("topicMsg2 = " + msg);
    }


}
